﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;

namespace ProjectSem40701.Admin
{
    public partial class QuestionEdit : System.Web.UI.Page
    {
        int qid;
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                qid = Convert.ToInt32(Request.QueryString["qid"].ToString().Trim());
                AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter dp =
                    new AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter();
                DataTable dttbl = dp.Get_tbl_Question_Select_All_By_QuestionID(qid);
                this.txtTitle.Text = dttbl.Rows[0]["QuestionTittle"].ToString();
                this.txtQuestionMark.Text = dttbl.Rows[0]["QuestionMark"].ToString();
                this.txtQuestionCreatedDate.Text = Convert.ToDateTime(dttbl.Rows[0]["QuestionCreatedDate"].ToString()).ToString("dd/MM/yyy");
                this.txtQuestionDescription.Text = dttbl.Rows[0]["QuestionDescription"].ToString();
                this.txtQuestionContent.Value = Server.HtmlDecode(dttbl.Rows[0]["QuestionContent"].ToString());

                foreach (ListItem item in this.cbbQuestionLevelID.Items)
                {
                    if (item.Value == dttbl.Rows[0]["QuestionLevelID"].ToString())
                    {
                        item.Selected = true;
                    }
                }

                foreach (ListItem item in this.cbbStatus.Items)
                {
                    if (item.Value == dttbl.Rows[0]["QuestionStatus"].ToString())
                    {
                        item.Selected = true;
                    }
                }

                FillAnswer();
            }
            catch
            {
                //show error here
            }
        }
        //Fill du lieu vao gridview chua cau tra loi
        private void FillAnswer()
        {
            try
            {
                qid = Convert.ToInt32(Request.QueryString["qid"].ToString().Trim());
                AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter dpAnswer =
                    new AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter();
                DataTable dttblAnswer = dpAnswer.Get_tbl_Answer_Select_All_By_QuestionID(qid);
                this.grvAnswers.DataSource = dttblAnswer;
                this.grvAnswers.DataBind();
                //Session["qid"] = qid.ToString();
            }
            catch
            { 
                //bao loi
            }
        }

        protected void RadComboBox1_PreRender(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
                DateTime dtCreatedDate = Convert.ToDateTime(this.txtQuestionCreatedDate.Text.Trim());

                qid = Convert.ToInt32(Request.QueryString["qid"].ToString().Trim());
                AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter dp = 
                    new AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter();
                int ok = dp.Update_tbl_Question(Convert.ToInt32(this.cbbSubjectID.SelectedValue.Trim()), 
                    this.txtTitle.Text.Trim(), 
                    Server.HtmlEncode(this.txtQuestionContent.Value.Trim()), 
                    //Convert.ToDateTime(this.txtQuestionCreatedDate.Text.Trim()), 
                    dtCreatedDate,
                    this.txtQuestionDescription.Text.Trim(),
                    Convert.ToInt32(this.cbbQuestionLevelID.SelectedValue.Trim()), 
                    Convert.ToInt32(this.txtQuestionMark.Text.Trim()), 
                    Convert.ToInt32(this.cbbStatus.SelectedValue.Trim()), qid);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog(FAIL);
            }
        }

        /// <summary>
        /// xóa câu hỏi
        /// </summary>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                qid = Convert.ToInt32(Request.QueryString["qid"].ToString().Trim());
                AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter dp = new AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter();
                int ok = dp.tbl_Question_Delete(qid);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog_Del(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog_Del(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog_Del(FAIL);
            }
        }

        protected void btnAddAnswer_Click(object sender, EventArgs e)
        {
            qid = Convert.ToInt32(Request.QueryString["qid"].ToString().Trim());
            Response.Redirect("AnswerAdd.aspx?qid=" + qid);
        }

        protected void lblStatus_DataBinding(object sender, EventArgs e)
        {
            Label lblStatus = sender as Label;
            lblStatus.Text = (lblStatus.Text == "1" ? "Kích hoạt" : "Khóa");
        }

        protected void lblContent_DataBinding(object sender, EventArgs e)
        {
            Label lblContent = sender as Label;
            lblContent.Text = Server.HtmlDecode(lblContent.Text);
        }

        protected void cbbSubjectID_DataBound(object sender, EventArgs e)
        {
            try
            {
                qid = Convert.ToInt32(Request.QueryString["qid"].ToString().Trim());
                AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter dp =
                    new AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter();
                DataTable dttbl = dp.Get_tbl_Question_Select_All_By_QuestionID(qid);
                
                foreach (ListItem item in this.cbbSubjectID.Items)
                {
                    if (item.Value == dttbl.Rows[0]["SubjectID"].ToString())
                    {
                        item.Selected = true;
                    }
                }
            }
            catch
            { 
                //do nothing
            }

        }

        /// <summary>
        /// Close dialog here
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Question.aspx");
                    break;
                case FAIL:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// Show dialog trong trường hợp update
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Cập nhật thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Cập nhật thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, cập nhật thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// Show dialog trong trường hợp delete
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
